package com.lzs.chuku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzs.entity.OutorderDetail;
import com.lzs.entity.Pick;
import com.lzs.entity.PickDetail;
import com.lzs.chuku.mapper.OutorderDetailMapper;
import com.lzs.chuku.service.*;
import com.lzs.entity.Picktask;
import com.lzs.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/chuku/pick-detail")
public class PickDetailController {

    @Autowired
    private PicktaskService picktaskService;
    @Autowired
    private PickService pickService;
    @Autowired
    private PickDetailService pickDetailService;

    @Autowired
    private PicktaskDetailService picktaskDetailService;
    @Autowired
    private OutorderDetailMapper outorderDetailMapper;
    @PostMapping("addPickDetail")
    @ApiOperation("新增拣货明细")
    public R addPickDetail(@RequestBody PickDetail pickDetail){
        try {
            Pick pick = pickService.getById(pickDetail.getPid());
            Picktask picktask = picktaskService.getById(pick.getPicktaskid());
            Long commodityid = pickDetail.getCommodityid();
            Long locatorid = pickDetail.getLocatorid();
            Long warehourseid = pickDetail.getWarehourseid();
            QueryWrapper wrapper = new QueryWrapper<>();
            wrapper.eq("warehourseid", warehourseid);
            wrapper.eq("locatorid", locatorid);
            wrapper.eq("commodityid", commodityid);
            wrapper.eq("pid",pick.getId());
            OutorderDetail outorderdetail = outorderDetailMapper.findone(warehourseid,locatorid,commodityid,picktask.getOutorderid());
            if (outorderdetail==null) {
                return R.error().message("您选择的仓库，库位，商品和出库订单表中对应不一致");
            }
            PickDetail pickDetail1 = pickDetailService.getOne(wrapper);
            if (pickDetail1!=null){
                Integer num=pickDetail1.getQty() + pickDetail.getQty();
                if (num>outorderdetail.getQty()){
                    return R.error().message("您分拣的数量大于订单中需要的数量");
                }else{
                    pickDetail1.setQty(num);
                    pickDetailService.saveOrUpdate(pickDetail1);
                }
            }else{
                if(pickDetail.getQty()>outorderdetail.getQty()){
                    return R.error().message("您分拣的数量大于订单中需要的数量");
                }
                pickDetailService.saveOrUpdate(pickDetail);
            }

            return R.ok().message("新增成功");
        }catch (Exception e){
            return R.error().message("新增失败");
        }
    }
    
    @GetMapping("findPickdetailByPid/{id}")
    @ApiOperation("根据拣货id查询明细")
    public R findPickdetailByPid(@PathVariable("id") Long id){
        try {
            QueryWrapper<PickDetail> wrapper = new QueryWrapper<>();
            wrapper.eq("pid", id);
            List<PickDetail> list = pickDetailService.list(wrapper);
            return R.ok().message("查询成功").data("list",list);
        }catch (RuntimeException e){
            return R.error().message("查询失败");
        }
    }
}

